package com.example.wanandroid.ui.fragment;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wanandroid.R;
import com.example.wanandroid.core.bean.main.collect.FeedArticleData;
import com.example.wanandroid.core.bean.navigation.NavigationListData;
import com.example.wanandroid.ui.adapter.ArticleListAdapter;
import com.example.wanandroid.ui.adapter.NavAuthorItemAdapter;
import com.example.wanandroid.ui.refresh_ui.LoadNextFooter;
import com.example.wanandroid.ui.refresh_ui.LoadPreHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.zhengsr.tablib.bean.TabConfig;
import com.zhengsr.tablib.view.adapter.TabFlowAdapter;
import com.zhengsr.tablib.view.flow.TabFlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangyi
 */
public class NavigationListFragment extends BaseFragment {
    private String TAG = "NavigationListFragment";
    NavigationListData navListData;
    int position;
    View fragmentView;
    RecyclerView navItemList;
    TextView textView;
    TabFlowLayout authorTabs;


    LinearLayoutManager layoutManager;
    ArticleListAdapter articleListAdapter;


    public static NavigationListFragment newInstance(NavigationListData navListData,int position){
        NavigationListFragment fragment = new NavigationListFragment();
        fragment.setNavListData(navListData);
        fragment.setPosition(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("navListData",navListData);
        bundle.putInt("position",position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void showLoading() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentView = inflater.inflate(R.layout.nav_list_fragment,container,false);
        Log.d(TAG, "onCreateView: "+navListData.getName());
        initFragment();
        return fragmentView;
    }

    private void initFragment(){
        if(getArguments()!=null){
            position = getArguments().getInt("position");
            navListData = (NavigationListData)getArguments().getSerializable("navListData");
        }
        navItemList = fragmentView.findViewById(R.id.nav_item_list);
        textView = fragmentView.findViewById(R.id.category_text);
        authorTabs = fragmentView.findViewById(R.id.rectflow);

        textView.setText(navListData.getName());

        layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        articleListAdapter = new ArticleListAdapter(getActivity());

        List<FeedArticleData> list = navListData.getArticles();

        Collections.sort(list, Comparator.comparingInt(o -> o.getAuthor().length()!=0? o.getAuthor().charAt(0):0));
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getAuthor().equals("")) {
                list.get(i).setAuthor("在线博客");
            }
        }
        navListData.setArticles(list);
        articleListAdapter.addDataList(new ArrayList<>(navListData.getArticles()));

        navItemList.setLayoutManager(layoutManager);
        navItemList.setAdapter(articleListAdapter);
        navItemList.setNestedScrollingEnabled(true);

        authorTabs.setTabConfig(new TabConfig.Builder().setTextId(R.id.author_text).build());

        List<String> authorList = navListData.getArticles().stream().map(FeedArticleData::getAuthor)
                .distinct()
                .collect(Collectors.toList());

        authorTabs.setAdapter(new NavAuthorItemAdapter(R.layout.textview_item,authorList,getContext()){
            @Override
            public void onItemClick(View view, String data, int position) {
                super.onItemClick(view, data, position);
                ((LinearLayoutManager)navItemList.getLayoutManager()).scrollToPositionWithOffset(position,0);
            }
        });
        navItemList.setOnScrollChangeListener((v,x,y,oldX,oldY)->{
            int position = layoutManager.findFirstVisibleItemPosition();
            String author = navListData.getArticles().get(position).getAuthor();
            int tabPosition = authorList.indexOf(author);
            authorTabs.setItemClickByOutside(tabPosition);
        });
    }

    public NavigationListData getNavListData() {
        return navListData;
    }

    public void setNavListData(NavigationListData navListData) {
        this.navListData = navListData;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        navItemList = null;
        textView = null;
        authorTabs = null;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

